
#include "stdafx.h"
#include "tmainwindow.h"
#include "tplayerview.h"
#include "tplaycontrolbar.h"


TPlayControlBar::TPlayControlBar(QWidget* parent) 
    : QWidget(parent)
    ,m_dType(DT_Time)
{
    m_mainWindow = static_cast<QWidget*>(parent->parent());
    setFixedHeight(CONTROLBAR_HEIGHT);

    setAttribute(Qt::WA_StyledBackground);
    setStyleSheet("border-image:url(:res/images/topbg.png) 5;\
                  border-width:0px;");

    m_stopButton = new QToolButton(this);
    m_stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
    connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(Stop()));

    m_playButton = new QToolButton(this);
    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    connect(m_playButton, SIGNAL(clicked()), this, SIGNAL(PlayPause()));

    m_playSlider = new QSlider(Qt::Horizontal, this);
    connect(m_playSlider, SIGNAL(sliderMoved(int)), this, SIGNAL(Seek(int)));

    m_timeLabel = new QLabel("00:00:00/00:00:00",this);
     
    m_muteButton = new QToolButton(this);
    m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
    connect(m_muteButton, SIGNAL(clicked()), this, SLOT(MuteClicked()));

    m_volumeSlider = new QSlider(Qt::Horizontal, this);
    m_volumeSlider->setRange(0, 100);
    connect(m_volumeSlider, SIGNAL(sliderMoved(int)), this, SIGNAL(ChangeVolume(int)));

    m_fullScreenButton = new QToolButton(this);
    m_fullScreenButton->setIcon(QIcon(QPixmap(":res/images/fullscreen.png")));
    connect(m_fullScreenButton, SIGNAL(clicked()), m_mainWindow, SLOT(FullScreen()));

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(5, 0, 5, 0);
    mainLayout->addWidget(m_stopButton, 1);
    mainLayout->addWidget(m_playButton, 1);
    mainLayout->addWidget(m_playSlider, 10);
    mainLayout->addWidget(m_timeLabel);
    mainLayout->addWidget(m_muteButton, 1);
    mainLayout->addWidget(m_volumeSlider, 1);
    mainLayout->addWidget(m_fullScreenButton, 1);
}

TPlayControlBar::~TPlayControlBar()
{

}

void TPlayControlBar::SetDurationType(DurationType dt)
{
    m_dType = dt;
}

void TPlayControlBar::SetDuration(int duration)
{
    m_playSlider->setRange(0, duration);
    m_playSlider->setValue(0);
}

void TPlayControlBar::SetCurrentPosition(int pos)
{
    int duration = m_playSlider->maximum();
    if(pos < 0) 
        pos = 0;
    if(pos > duration)
        pos = duration;

    m_playSlider->setValue(pos);
    if(DT_Time == m_dType)
    {
        QTime totalTime(duration / 60 / 24 , duration /60 % 60 , duration % 60);
        QTime elapseTime(pos / 60 / 24, pos / 60 % 60, pos % 60);
        m_timeLabel->setText(elapseTime.toString() + "/" + totalTime.toString());
    }
    else
    {
        m_timeLabel->setText(QString::number(pos) + "/" + QString::number(duration));
    }
}

void TPlayControlBar::SetCurrentVolume(int volume)
{
    m_volumeSlider->setValue(volume);
}

void TPlayControlBar::OnPlayPause(bool bPlay)
{
    if(bPlay)
        m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
    else
        m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
}